﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.CommonHelpers;

namespace WHControlLib.Controls.Goods
{
    [ToolboxItem(false)]
    public partial class WHGoodsInfo : baseStaticCtrl
    {

        Size OldSize;
        Size NewSize;
        enum StrDec
        {
            Left,Centr,Right
        }
        

        Rectangle SeialRect=new Rectangle();
        Rectangle GoodsBarcodeRect=new Rectangle();
        Rectangle GoodsNameRect=new Rectangle();
        Rectangle GoodsNowPriceRect=new Rectangle();    
        Rectangle GoodsDiscoutsInfoRect=new Rectangle();
        Rectangle GoodsPriceSumRect=new Rectangle();
        Rectangle GoodsUnitNumberRect=new Rectangle();
        Rectangle OtherInfoRect = new Rectangle();


        #region 属性定义 

        private Color _sumPriceColor=Color.Red;

       /// <summary>
       /// 当前单价字体颜色
       /// </summary>
        public Color SumPriceColor
        {
            get { return _sumPriceColor; }
            set { _sumPriceColor = value; }
        }



        private Color _elementFontColor=Color.Black;
        /// <summary>
        /// 信息字体颜色
        /// </summary>
        public Color ElementFontColor
        {
            get { return _elementFontColor; }
            set { _elementFontColor = value; }
        }
        private Color _elementDicountFontColor = Color.Red;
        /// <summary>
        /// 优惠信息字体颜色
        /// </summary>
        public Color ElementDicountFontColor
        {
            get { return _elementDicountFontColor; }
            set { _elementDicountFontColor = value; }
        }

        private bool _isseleced;
        /// <summary>
        /// 是否处于选中状态
        /// </summary>
        public bool IsSelected
        {
            get { return _isseleced; }
            set {

                if (value)
                {

               
                IsSelectedMy = true;

                    try
                    {
                        if (Parent == null) return;

                        foreach (Control item in this.Parent.Controls)
                        {
                            if (item == this)
                            {
                                continue;
                            }
                            if (item is WHGoodsInfo)
                            {
                        
                                this.Invoke(new MethodInvoker(() =>
                                {
                                    WHGoodsInfo WH    = (WHGoodsInfo)item;
                                
                                   WH.IsSelectedMy = false;
                                }));


                            }
                        }

                    }
                    catch (Exception)
                    {

                        //throw;
                    }



                }

                Invalidate();

                _isseleced = value; }
        }





        /// <summary>
        /// 该控件对应在列表中的序号
        /// </summary>
        public int ThisGoodsIndex { get; set; }


        public bool ShowGoodsOldPrice;
        public bool ShowGoodsDiscountInfo;
        public bool ShowGoodsGiftInfo;


        private string _serialStr;

        public string SerialStr
        {
            get { return _serialStr; }
            set
            {
             
                _serialStr = value;
            }
        }


        private string _goodsBarcodeStr;

        public string GoodsBarcodeStr
        {
            get { return _goodsBarcodeStr; }
            set
            {
          
                _goodsBarcodeStr = value;
            }
        }
        private string _goodsNameStr;

        public string GoodsNameStr
        {
            get { return _goodsNameStr; }
            set
            {

                _goodsNameStr = value;
            }
        }

        private string _goodsNowPriceStr;

        public string GoodsNowPriceStr
        {
            get { return _goodsNowPriceStr; }
            set
            {
          
                _goodsNowPriceStr = value;
            }
        }
        private string _goodsOldPriceStr;
        public string GoodsOldPriceStr
        {
            get { return _goodsOldPriceStr; }
            set
            {

                _goodsOldPriceStr = value;
            }
        }
        private string _goodsDicountStr;

        public string GoodsDicountStr
        {
            get { return _goodsDicountStr; }
            set
            {
          
                _goodsDicountStr = value;
            }
        }

        private string _goodsPriceSumStr;

        public string GoodsPriceSumStr
        {
            get { return _goodsPriceSumStr; }
            set
            {
      

                _goodsPriceSumStr = value;
            }
        }

        private string _numberAndUnitStr;

        public string NumberAndUnitStr
        {
            get { return _numberAndUnitStr; }
            set
            {

                _numberAndUnitStr = value;
            }
        }

        private string _otherInfoStr;

        public string OtherInfoStr
        {
            get { return _otherInfoStr; }
            set
            {


                _otherInfoStr = value;
            }
        }



        #endregion

        void SetRects(Rectangle  MyRct)
        {
            //序号 坐标和大小
            SeialRect.Width=MyRct.Width/10;
            SeialRect.Height = MyRct.Height / 4;
            Point SeialP = new Point(0, (MyRct.Height-SeialRect.Height)/2);
            SeialRect.Location= SeialP;
            // 编号 坐标和大小
            GoodsBarcodeRect.Width =(int)(MyRct.Width / 10 * 4.7);
            GoodsBarcodeRect.Height = (int)(MyRct.Height / 3.3);
            Point barcodeP=new Point(3+SeialRect.Width, 2);
            GoodsBarcodeRect.Location = barcodeP;
            // 名称 坐标和大小
            GoodsNameRect.Width= MyRct.Width / 10 *4;
            GoodsNameRect.Height = MyRct.Height / 3;
            Point NameP = new Point(GoodsBarcodeRect.Right + 5, GoodsBarcodeRect.Y);
            GoodsNameRect.Location = NameP;
            // 单价 坐标和大小
            GoodsNowPriceRect.Width = GoodsBarcodeRect.Width;
            GoodsNowPriceRect.Height = GoodsBarcodeRect.Height;
            Point NowPriceP = new Point(GoodsBarcodeRect.X,GoodsBarcodeRect.Y+ GoodsBarcodeRect.Height);
            GoodsNowPriceRect.Location = NowPriceP;
            // 优惠信息 坐标和大小
            GoodsDiscoutsInfoRect.Width =GoodsNameRect.Width;
            GoodsDiscoutsInfoRect.Height= GoodsNameRect.Height;
            Point DiscountsP = new Point(GoodsNameRect.X - GoodsDiscoutsInfoRect.Width/5, GoodsNowPriceRect.Y);
            GoodsDiscoutsInfoRect.Location = DiscountsP;


            // 小计 坐标和大小
            GoodsPriceSumRect.Width=GoodsBarcodeRect.Width;
            GoodsPriceSumRect.Height = GoodsBarcodeRect.Height;
            Point SumRectP=new Point(GoodsBarcodeRect.X,GoodsNowPriceRect.Y+GoodsNowPriceRect.Height);
            GoodsPriceSumRect.Location = SumRectP;
            // 数量单位 坐标和大小
            GoodsUnitNumberRect.Width=GoodsNameRect.Width/2;
            GoodsUnitNumberRect.Height=GoodsNameRect.Height;
            Point UnitNumberP = new Point(GoodsPriceSumRect.X + GoodsPriceSumRect.Width, GoodsPriceSumRect.Y);
            GoodsUnitNumberRect.Location = UnitNumberP;
            // 其他信息 坐标和大小
            OtherInfoRect.Width = GoodsUnitNumberRect.Width;
            OtherInfoRect.Height = GoodsUnitNumberRect.Height;
            Point otherP = new Point(GoodsUnitNumberRect.X + GoodsUnitNumberRect.Width, GoodsUnitNumberRect.Y);
            OtherInfoRect.Location = otherP;

        }

        void DrawGoodsInfo(Graphics myg,string Str,Color StrColor,Rectangle StrRect,float fontSize,StrDec strDec)
        {
            StringFormat sf = new StringFormat();
            switch (strDec)
            {
                case StrDec.Left:
                    sf.Alignment = StringAlignment.Near; 
                    break;
                case StrDec.Centr:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case StrDec.Right:
                    sf.Alignment = StringAlignment.Far;
                    break;
                default:
                    break;
            }
          
            sf.LineAlignment = StringAlignment.Center;
            Font myfont=new Font(this.Font.FontFamily,fontSize);
            using (SolidBrush sb =new SolidBrush(StrColor))
                     
            {
                myg.DrawString(Str, myfont, sb, StrRect, sf);
            }
           
            
        }
        public WHGoodsInfo()
        {
            //设置双缓冲开启禁止背景擦拭
            SetStyle(
                     ControlStyles.OptimizedDoubleBuffer
                     | ControlStyles.ResizeRedraw
                     | ControlStyles.Selectable
                     | ControlStyles.AllPaintingInWmPaint
                     | ControlStyles.UserPaint
                     | ControlStyles.SupportsTransparentBackColor,
                     true);

          
          
            InitializeComponent();
            IsUseSelectState=true;
        }

         


        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    Rectangle MyRect = this.ClientRectangle;
           
           

        //    Graphics Myg = e.Graphics;
        //    Myg.SmoothingMode = SmoothingMode.AntiAlias;
        //    Myg.CompositingQuality = CompositingQuality.HighQuality;
        //    Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
           
        //   //画出背景


          
             
            
            

        //}
        public override void BeforePaintExtendMethod(Rectangle Rect)
        {
            SetRects(Rect);
        }

        public override void ChildenExtendPaintMethod(Graphics g)
        { //画出每个元素
            DrawEveryElement(g);
            
        }
        void DrawEveryElement(Graphics Myg)
        {

            //画 序号 ElementFontColor
            DrawGoodsInfo(Myg, SerialStr, ElementFontColor, SeialRect, this.Font.Size, StrDec.Centr);
           
            //画  编号
            DrawGoodsInfo(Myg, GoodsBarcodeStr, ElementFontColor, GoodsBarcodeRect, this.Font.Size, StrDec.Left);
           
            //画  名称
            DrawGoodsInfo(Myg, GoodsNameStr, ElementFontColor, GoodsNameRect, this.Font.Size, StrDec.Left);
           
            //画 单价
            DrawGoodsInfo(Myg, GoodsNowPriceStr, ElementFontColor, GoodsNowPriceRect, this.Font.Size, StrDec.Left);
            
            //画  优惠信息
      
            DrawOldPricre(Myg, GoodsOldPriceStr, ShowGoodsOldPrice);
            DrawGoodsDiscount(Myg , GoodsDicountStr, ShowGoodsDiscountInfo);
            DrawGiftInfo(Myg, ShowGoodsGiftInfo);
            
            //画 小计

            DrawGoodsInfo(Myg, GoodsPriceSumStr, SumPriceColor, GoodsPriceSumRect, this.Font.Size, StrDec.Left);

            //画 数量 单位
            DrawGoodsInfo(Myg, NumberAndUnitStr, ElementFontColor, GoodsUnitNumberRect, this.Font.Size, StrDec.Left);
            //画 其他信息
            DrawGoodsInfo(Myg, OtherInfoStr, ElementFontColor, OtherInfoRect, this.Font.Size, StrDec.Left);

            //Myg.DrawRectangle(Pens.Blue, SeialRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsBarcodeRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsNameRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsNowPriceRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsDiscoutsInfoRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsPriceSumRect);
            //Myg.DrawRectangle(Pens.Blue, GoodsUnitNumberRect);
            //Myg.DrawRectangle(Pens.Blue, OtherInfoRect);


        }
        void DrawGoodsDiscount(Graphics g, string GoodsdiccountStr,bool showDiscount)
        {
            if (showDiscount && GoodsdiccountStr!=null && !ShowGoodsGiftInfo)
            {
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
             SizeF s=   g.MeasureString(GoodsdiccountStr, this.Font);

                int W = (int)s.Width+2;
                int H = (int)s.Height+2;
                Rectangle rect = new Rectangle();
                rect.X = OtherInfoRect.X + 2;
                rect.Y = OtherInfoRect.Y;
                rect.Width = W;
                rect.Height=OtherInfoRect.Height;
             
               
                using (SolidBrush sb = new SolidBrush(ElementDicountFontColor))

                {
                    g.DrawString(GoodsdiccountStr, this.Font, sb, rect, sf);


                }
                using (Pen pen = new Pen(ElementDicountFontColor, 2))
                {
                    
                    Rectangle re=new Rectangle ();
                    re.X=rect .X;
                    re.Y = rect.Y + (rect.Height - (int)H) / 2;
                    re.Width = W;
                    re.Height = H;
                    g.DrawPath(pen, DrawHelper.GetRoundRectangePath(re, 0, 5));
                }


            }

        }

        void DrawOldPricre(Graphics g, string OldPriceStr,bool showOldPrice)
        {
            if (showOldPrice && OldPriceStr != null)
       
        
            {
                Rectangle rect=new Rectangle();
                //rect.X = GoodsNowPriceRect.X+ (int)(g.MeasureString(GoodsNowPriceStr,this.Font).Width)+3;
                rect.X=GoodsDiscoutsInfoRect.X;
                rect.Y = GoodsDiscoutsInfoRect.Y;
                rect.Width=GoodsBarcodeRect.Width;
                rect.Height = GoodsDiscoutsInfoRect.Height;
               
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near ;
                sf.LineAlignment = StringAlignment.Center;

                string s = "原价:￥" + OldPriceStr;
                SizeF f=  g.MeasureString(s, this.Font);


                StringFormat sf2 = new StringFormat();
                sf2.Alignment = StringAlignment.Far;
                sf2.LineAlignment = StringAlignment.Center;
                string gstr = "改";
                SizeF ff = g.MeasureString(gstr, this.Font);
                Rectangle gStrRect = new Rectangle();
                gStrRect.X = rect.X + (int)f.Width+(int)(GoodsDiscoutsInfoRect.Width*0.12);
                gStrRect.Y = GoodsDiscoutsInfoRect.Y + (GoodsDiscoutsInfoRect.Height - (int)ff.Height) / 2;
                gStrRect.Width = (int)ff.Width + 2;
                gStrRect.Height = (int)ff.Height +2;
                
                  using (SolidBrush sb = new SolidBrush(ElementDicountFontColor))

                {  
                    g.DrawString(s, this.Font, sb, rect, sf);

                    g.DrawString(gstr, this.Font, sb, gStrRect, sf);


                } 
                
                using (Pen pen=new Pen(ElementDicountFontColor, 2))
                {
               
                    g.DrawLine(pen , new Point(rect.X, rect.Y + rect.Height / 2), new Point(rect.X +(int) f.Width, rect.Y + rect.Height / 2));

                  g.DrawPath(pen,DrawHelper.GetRoundRectangePath(gStrRect, 0, 5)) ;
                }


            }
        
        
        }

       void  DrawGiftInfo(Graphics g,bool showGiftInfo)
        {
            if (showGiftInfo)
            {
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                SizeF s = g.MeasureString("赠", this.Font);

                int W = (int)s.Width + 2;
                int H = (int)s.Height + 2;
                Rectangle rect = new Rectangle();
                rect.X = OtherInfoRect.X + 2;
                rect.Y = OtherInfoRect.Y;
                rect.Width = W;
                rect.Height = H;

               
                using (SolidBrush sb = new SolidBrush(ElementDicountFontColor))

                {
                  
                    g.DrawString("赠", this.Font, sb, rect, sf);


                }
                using (Pen pen = new Pen(ElementDicountFontColor, 2))
                {
                   


                    g.DrawPath(pen, DrawHelper.GetRoundRectangePath(rect, 0, 5));
                }


            }

        }

        private void WHGoodsInfo_Load(object sender, EventArgs e)
        {
        
          

        }

        private void WHGoodsInfo_SizeChanged(object sender, EventArgs e)
        {
         
           

        }

     
        private void WHGoodsInfo_Resize(object sender, EventArgs e)
        {
           
        }

        private void WHGoodsInfo_Layout(object sender, LayoutEventArgs e)
        {
            OldSize.Width = this.Width;
            OldSize.Height = this.Height;
        }


        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

           
            
         


        }
        private void WHGoodsInfo_Click(object sender, EventArgs e)
        {
            
            ThisGoodsIndex = Convert.ToInt32(SerialStr);

          IsSelected = true; 
        }

        ////////////////////////////////////
    }
}
